#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>

struct TreeNode {
      int val;
     struct TreeNode *left;
     struct TreeNode *right;
  };
 
void postorder(struct TreeNode* root, int** a, int* returnSize, int* k) {
    if (root == NULL)
        return;
    
    postorder(root->left, a, returnSize, k);
    postorder(root->right, a, returnSize, k);
    if ((*returnSize) >= (*k)) {
        int* new = realloc(*a, (*k) * 2 * sizeof(int));
        if (new != NULL) {
            *a = new;
            (*k) *= 2;
        }
        else {
            printf("realloc failure\n");
            exit(-1);
        }
    }
    (*a)[(*returnSize)++] = root->val;

}
int* postorderTraversal(struct TreeNode* root, int* returnSize) {
    int* a = malloc(4 * sizeof(int));
    int* k = malloc(sizeof(int));
    *k = 4;
    *returnSize = 0;
    postorder(root, &a, returnSize, k);
    return a;
}
void p1() {
    struct TreeNode* p1 = malloc(sizeof(struct TreeNode));
    p1->val = 1;
    p1->left = NULL;

    struct TreeNode* p2 = malloc(sizeof(struct TreeNode));
    p2->right = NULL;
    p2->val = 2;
    p1->right = p2;

    struct TreeNode* p3 = malloc(sizeof(struct TreeNode));
    p2->left = p3;
    p3->val = 3;
    p3->left = NULL;
    p3->right = NULL;

    int* l = malloc(sizeof(int));

    int* a=postorderTraversal(p1, l);
    int k = sizeof(a) / sizeof(int);
    for (int i = 0; i < 3; i++) {
        printf("%d ", a[i]);
    }
}
int main() {
    p1();
}